import { CARD, PokerCard } from "./card";
import { PokerSet } from "./set";

// 定义一副扑克牌
export class PokerDeck {

  private _cards: Array<PokerCard>;

  constructor() {
    this._cards = new Array<PokerCard>(54);
  }

  public get cards() {
    return this._cards;
  }

  // 获取指定索引的扑克牌
  public GetCard(index: number): PokerCard {
    return this._cards[index];
  }

  // 计算扑克牌的数量
  public CountCards(): number {
    return this._cards.length;
  }

  // 获取该deck中的所有牌
  public GetAllCards(): Array<PokerCard> {
    return this._cards;
  }

  // 将pokerdeck转换为pokerset
  public ToPokerSet(): PokerSet {
    const set = new PokerSet();
    set.AddPokers(this._cards);
    return set;
  }
}

// 每个游戏桌子都有单独的扑克牌, 防止洗牌等操作冲突
export function CreateDeck(): PokerDeck {
  return createOriginDeck();
}

// 根据输入的扑克副数, 生成扑克集
export function CreatePokerSetWithDeckNum(deckNum: number): PokerSet {
  const set = new PokerSet();
  for (let i = 0; i < deckNum; i++) {
    set.AddPokers(CreateDeck().cards);
  }
  return set;
}

// 创建原始扑克牌, 后续只需要复制即可, 不用再运算获得
function createOriginDeck(): PokerDeck {
  const deck = new PokerDeck();
  for (let i = 0; i < 52; i++) {
    const shang = i / 4;
    const yu = i % 4;
    let suit = "";
    switch (yu) {
      case 0: suit = CARD.SUIT.SPADE; break;
      case 1: suit = CARD.SUIT.HEART; break;
      case 2: suit = CARD.SUIT.CLUB; break;
      case 3: suit = CARD.SUIT.DIAMOND; break;
      default: break;
    }
    let pokerValue = 0;
    let pokerName = "";
    switch (shang) {
      case 0: pokerValue = CARD.VALUE.THREE; pokerName = CARD.SYMBOL.THREE; break;
      case 1: pokerValue = CARD.VALUE.FOUR; pokerName = CARD.SYMBOL.FOUR; break;
      case 2: pokerValue = CARD.VALUE.FIVE; pokerName = CARD.SYMBOL.FIVE; break;
      case 3: pokerValue = CARD.VALUE.SIX; pokerName = CARD.SYMBOL.SIX; break;
      case 4: pokerValue = CARD.VALUE.SEVEN; pokerName = CARD.SYMBOL.SEVEN; break;
      case 5: pokerValue = CARD.VALUE.EIGHT; pokerName = CARD.SYMBOL.EIGHT; break;
      case 6: pokerValue = CARD.VALUE.NINE; pokerName = CARD.SYMBOL.NINE; break;
      case 7: pokerValue = CARD.VALUE.TEN; pokerName = CARD.SYMBOL.TEN; break;
      case 8: pokerValue = CARD.VALUE.JACK; pokerName = CARD.SYMBOL.JACK; break;
      case 9: pokerValue = CARD.VALUE.QUEEN; pokerName = CARD.SYMBOL.QUEEN; break;
      case 10: pokerValue = CARD.VALUE.KING; pokerName = CARD.SYMBOL.KING; break;
      case 11: pokerValue = CARD.VALUE.ACE; pokerName = CARD.SYMBOL.ACE; break;
      case 12: pokerValue = CARD.VALUE.TWO; pokerName = CARD.SYMBOL.TWO; break;
      default: break;
    }
    deck.cards[i] = new PokerCard(pokerValue, suit, pokerName);
  }
  deck.cards[52] = new PokerCard(CARD.VALUE.BLACK_JOKER, CARD.SUIT.JOKER, CARD.SYMBOL.BLACK_JOKER);
  deck.cards[53] = new PokerCard(CARD.VALUE.RED_JOKER, CARD.SUIT.JOKER, CARD.SYMBOL.RED_JOKER);

  return deck;
}
